<template>
  <div>
    <div>【服务资源占用比】</div>
    <div class="w-full h-full" ref="target"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch ,nextTick } from "vue";
import * as echarts from "echarts";
const props = defineProps({
  datas: {
    type: Array,
    requierd: true,
  },
});
console.log(props.datas);
// 1.初始化 echarts 实例
let myChart = null;
const target = ref(null);
// 2.构建 option 对象
const renderChart = () => {
  const options = {
    // x 轴展示数据
    xAxis: {
      // 展示数据
      type: "category",
      data: props.datas.map((item) => item.name),
      axisLabel: {
        color: "#9eb1c8",
      },
      interval:50
    },
    // y 轴展示数据
    yAxis: {
      show: false,
      type: "value",
      // 最大值
      max: function (value) {
        return parseInt(value.max * 1.2);
      },
    },
    // 图表绘制位置
    grid: {
      top: 16,
      left: -26,
      right: 0,
      bottom: 26,
      containLabel: true,
    },
    // 核心配置
    series: [
      {
        type: "bar",
        data: props.datas.map((item) => {
          return {
            name: item.name,
            value: item.value,
          };
        }),
        barWidth: 20,
        itemStyle: {
          color: "#5d98ce",
          borderRadius: [5, 5, 0, 0],
          shadowColor: "rgba(0,0,0,0.3)",
        },
        label: {
          show: true,
          position: "top",
          formatter: "{c}%",
          textStyle: {
            color: "#fff",
            fontSize: 12,
          },
        },
      },
    ],
  };
  // 3.通过 实例.setOption() 方法，将 option 对象挂载到实例上
  myChart.setOption(options);
};

watch(() => props.datas, ()=>{
    nextTick(()=>{
        renderChart();
    })
});
onMounted(() => {
  myChart = echarts.init(target.value);
  renderChart();
});
</script>

<style lang="scss" scoped></style>
